package com.haibei.net;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.json.JSONException;

import com.common.net.NetResult;
import com.common.uitl.LogUtil;
import com.haibei.App;

public class NetParser {

	// 批量删除文章
	public static NetResult parseBatchRemove(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseBatchRemove(jsonString);
		}
	}

	// 列出商品详情
	public static NetResult parseGoodsDetail(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseGoodsDetail(jsonString);
		}
	}

	// 列出所有商品
	public static NetResult parseListGoods(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseGoodsList(jsonString);
		}
	}

	public static NetResult parseBrand(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseBrand(jsonString);
		}
	}

	public static NetResult parseRegion(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseRegion(jsonString);
		}
	}

	/**
	 * 解析密码修改
	 * 
	 * @param inputs
	 * @return
	 * @throws JSONException
	 */
	public static NetResult modifyPassword(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseChangePassWord(jsonString);
		}
	}

	/**
	 * 修改用户头像
	 * 
	 * @param inputs
	 * @return
	 * @throws JSONException
	 */
	public static NetResult modifyUserHeader(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			// LogUtil.e(App.tag, jsonString);
			return JsonParser.parseRegist(jsonString);
		}
	}

	public static NetResult parseRegist(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			// LogUtil.e(App.tag, jsonString);
			return JsonParser.parseRegist(jsonString);
		}
	}

	public static NetResult parseLogin(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			// LogUtil.e(App.tag, jsonString);
			return JsonParser.parseLogin(jsonString);
		}
	}

	/**
	 * 获取文章类别
	 * 
	 * @param inputs
	 * @return
	 * @throws JSONException
	 */
	public static NetResult parseArticleCategory(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseArticleCategory(jsonString);
		}
	}

	public static NetResult parseArticle(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseArticles(jsonString);
		}
	}

	// 解析收藏过的文章列表
	public static NetResult parseListCollectArticle(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseCollectArticles(jsonString);
		}
	}

	// 解析收藏的商品
	public static NetResult parseCollectGoods(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseCollectArticles(jsonString);
		}
	}

	// 解析消息列表
	public static NetResult parseListMessage(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseMessage(jsonString);
		}
	}

	public static NetResult parsrArticleDetail(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseArticleDetail(jsonString);
		}
	}

	/**
	 * 热门评论
	 * 
	 * @param inputs
	 * @throws JSONException
	 */
	public static NetResult parsrComment1(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseArticleComment1(jsonString);
		}
	}

	/**
	 * 解析收藏文章
	 * 
	 * @param inputs
	 * @return
	 * @throws JSONException
	 */
	public static NetResult parseCollectArticle(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseCollectArticle(jsonString);
		}
	}

	// 解析收藏商品
	public static NetResult parseCollectWare(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseCollectWare(jsonString);
		}
	}

	/**
	 * 解析子评论
	 * 
	 * @param inputs
	 * @throws JSONException
	 */
	public static NetResult parseSubComment(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseSubComment(jsonString);
		}
	}

	/**
	 * 普通评论-全部评论
	 * 
	 * @param inputs
	 * @throws JSONException
	 */
	public static NetResult parsrComment2(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseAllComment(jsonString);
		}
	}

	/**
	 * 普通评论-全部评论
	 * 
	 * @param inputs
	 * @throws JSONException
	 */
	public static NetResult parsrParse(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parsePraise(jsonString);
		}
	}

	public static NetResult parseTopic(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseTopics(jsonString);
		}
	}

	public static NetResult parseDetailTopic(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseDetailTopic(jsonString);
		}
	}

	/**
	 * 解析评论
	 * 
	 * @param inputs
	 * @return
	 * @throws JSONException
	 */
	public static NetResult parseCommentTopic(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseTopicComment(jsonString);
		}
	}

	/**
	 * 解析投票
	 * 
	 * @param inputs
	 * @return
	 * @throws JSONException
	 */
	public static NetResult parseVoteTopic(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseTopicVote(jsonString);
		}
	}

	// 关注、收藏话题
	public static NetResult parseAddTopic(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseAddTopic(jsonString);
		}
	}

	/**
	 * 解析Search
	 * 
	 * @param inputs
	 * @return
	 * @throws JSONException
	 */
	public static NetResult parseSearch(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseTopicVote(jsonString);
		}
	}

	// 商品分类
	public static NetResult parseParCategory(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parselistParCategory(jsonString);
		}
	}

	// 广告热销商品
	public static NetResult parseBanner(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseBanner(jsonString);
		}
	}

	// 收藏的topic
	public static NetResult parseCollectTopic(InputStream inputs) throws JSONException {
		if (null == inputs) {
			return null;
		} else {
			String jsonString = getNetJson(inputs);
			return JsonParser.parseCollectTopics(jsonString);
		}
	}

	/**
	 * 读取InputStream的json数据为字符串
	 * 
	 * @param ins
	 * @return
	 */
	public static String getNetJson(InputStream ins) {
		if (null != ins) {
			StringBuffer sbf = new StringBuffer();
			try {
				BufferedReader bfr = new BufferedReader(new InputStreamReader(ins));
				String line = null;
				while ((line = bfr.readLine()) != null) {
					sbf.append(line.trim());
				}
				bfr.close();
			} catch (Exception e) {
				e.printStackTrace();
				LogUtil.e(App.tag, "解析json出错");
			}
			return sbf.toString();
		} else {
			return "";
		}
	}
}
